package com.ding.easycontroller.controller;

import com.ding.easycontroller.model.R;
import com.ding.easycontroller.pojo.FilterBrief;
import com.ding.easycontroller.pojo.FilterDetails;
import com.ding.easycontroller.pojo.FilterUpdateOrAddVO;
import com.ding.easycontroller.service.FilterService;
import com.ding.easycontroller.work.Filter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/_config/filter")
public class _Filter {


    @Autowired
    private FilterService filterService;


    //删除
    @PostMapping("/removeById")
    R remove(@RequestParam String id) {
        filterService.removeById(id);
        return R.OK;
    }

    //更新
    @PostMapping("/updateOrAdd")
    R updateOrAdd(@Validated FilterUpdateOrAddVO updateVO) {
        Filter filter = new Filter();
        BeanUtils.copyProperties(updateVO, filter);
        filterService.updateOrAdd(filter);
        return R.ok(filter.getId());
    }

    @GetMapping("/get")
    R get(@RequestParam String id) {
        Filter filter = filterService.get(id);
        if (filter == null) {
            return R.NotFound;
        }
        FilterDetails filterDetails = new FilterDetails();
        BeanUtils.copyProperties(filter, filterDetails);
        return R.ok(filterDetails);
    }

    @GetMapping("/all")
    R all(){
        List<Filter> all = filterService.getAll();
        List<FilterBrief> r = new ArrayList<>();
        for (Filter filter : all) {
            FilterBrief filterBrief = new FilterBrief();
            BeanUtils.copyProperties(filter, filterBrief);
            r.add(filterBrief);
        }
        return R.ok(r);
    }

}

